﻿using MessagingToolkit.QRCode.Codec;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;

namespace EPlusReport
{
    public class QRCODE
    {
        public static byte[] BitmapToBytes(Bitmap bmp)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
            byte[] byteImage = ms.ToArray();
            ms.Close();
            return byteImage;
        }

        public static System.IO.MemoryStream BitmapToStream(Bitmap bmp)
        {
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
            return ms;
        }

        public static void Save(string jpgfile, Image img)
        {
            try
            {
                System.IO.FileStream fs = System.IO.File.Open(jpgfile, System.IO.FileMode.Create);

                //System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(this.Width, this.Height);
                //Rectangle rect = new Rectangle(0, 0, this.Width, this.Height);
                //this.DrawToBitmap(bmp, rect);
                switch (jpgfile.Substring(jpgfile.IndexOf('.') + 1).ToLower())
                {
                    case "bmp":
                        img.Save(fs,
                           System.Drawing.Imaging.ImageFormat.Bmp);
                        break;

                    case "gif":
                        img.Save(fs,
                           System.Drawing.Imaging.ImageFormat.Gif);
                        break;
                    case "png":
                        img.Save(fs,
                           System.Drawing.Imaging.ImageFormat.Png);
                        break;
                    default:
                        img.Save(fs,
                           System.Drawing.Imaging.ImageFormat.Jpeg);
                        break;
                }
                fs.Close();
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }

        static Image LoadLogo(string file)
        {
            Image img = null; //new Bitmap();
            if (file.ToLower().EndsWith(".ico"))
            {
                System.Drawing.Icon ico = new Icon(file);
                img = ico.ToBitmap();
            }
            else
            {
                img = Image.FromFile(file);
            }
            return img;
        }

        public static Image DrawCode(string strcode, string Logo)
        {
            if (string.IsNullOrEmpty(strcode))
            {
                return null;
            }

            Image image = null;
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            String data = strcode;
            qrCodeEncoder.QRCodeVersion = 0;
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
            image = qrCodeEncoder.Encode(data);
            Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);

            if (!string.IsNullOrEmpty(Logo))
            {
                if (File.Exists(Logo))
                {
                    Image bmp = LoadLogo(Logo);
                    int ileft = (image.Width - bmp.Width) / 2;
                    int itop = (image.Height - bmp.Height) / 2;
                    Graphics g = Graphics.FromImage(image);

                    int imax = Math.Min(image.Width / bmp.Width, image.Height / bmp.Height);

                    if (imax < 3)
                    {
                        bmp = new Bitmap(bmp, new Size((int)(image.Width / 3), (int)(image.Height / 3)));
                        ileft = (image.Width - bmp.Width) / 2;
                        itop = (image.Height - bmp.Height) / 2;
                    }

                    g.DrawImage(bmp, ileft, itop);
                    bmp.Dispose();
                }
            }
            return image;
        }


        public static byte[] GetCode(string strcode, string Logo, int iheight)
        {
            Image image = null;
            QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
            String data = string.Concat(strcode);
            qrCodeEncoder.QRCodeVersion = 0;
            qrCodeEncoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
            image = qrCodeEncoder.Encode(data);
            Bitmap bmpcode = new Bitmap(image);
            Rectangle rect = new Rectangle(0, 0, image.Width, image.Height);
            if (!string.IsNullOrEmpty(Logo))
            {
                if (File.Exists(Logo))
                {
                    Image bmp = LoadLogo(Logo);
                    int ileft = (image.Width - bmp.Width) / 2;
                    int itop = (image.Height - bmp.Height) / 2;
                    Graphics g = Graphics.FromImage(image);

                    int imax = Math.Min(image.Width / bmp.Width, image.Height / bmp.Height);

                    if (imax < 3)
                    {
                        bmp = new Bitmap(bmp, new Size((int)(image.Width / 3), (int)(image.Height / 3)));
                        ileft = (image.Width - bmp.Width) / 2;
                        itop = (image.Height - bmp.Height) / 2;
                    }

                    g.DrawImage(bmp, ileft, itop);
                    bmp.Dispose();
                }
            }
            return BitmapToBytes(bmpcode);
        }

    }
}
